<template>
	<view class="cart">
		<js-navbar title="购物车" showEdit :edit="isedit" @handleEdit="handleEdit" bgColor="#fff" fontColor="#000"></js-navbar>
		<u-sticky bgColor="#fff" :offsetTop="NavBar.height"><js-search-bar v-model="keyword" @clear="clear" @search="search"></js-search-bar></u-sticky>

		<view class="container">
			<u-transition mode="fade-down" :show="!!list.length">
				<goodList :list="list" :isedit="isedit" @clickName="clickName" @update="update" @getSelect="getSelect" listStyle="5"></goodList>
			</u-transition>
		</view>
		<u-empty marginTop="66" v-if="list.length === 0" mode="car" icon="http://cdn.uviewui.com/uview/empty/car.png"></u-empty>

		<u-modal :show="del_modal" :title="`确定将这${selectList.length}个商品删除？`" @close="del_modal = false" :closeOnClickOverlay="true">
			<view class="u-modal-btnbox" slot="confirmButton">
				<button @click="del_modal = false">取消</button>
				<button @click="qrdel">确认</button>
			</view>
		</u-modal>

		<view class="jiesuanbox">
			<view class="jiesuan">
				<view class="box">
					<checkbox-group @change="quanxuan">
						<label>
							<checkbox :checked="isCheckedAll" />
							<text>全选</text>
						</label>
					</checkbox-group>
				</view>
				<view class="box2" v-if="!isedit">
					<text>合计：</text>
					<text>{{ total }}</text>
				</view>
				<view class="box3" v-if="!isedit">
					<button @click="jiesuan">结算</button>
				</view>

				<view class="box3" v-if="isedit">
					<button @click="shanchu">删除</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import API from '@/common/api';
import { updata_car_count } from '@/common/common_fun.js';
export default {
	name: 'cart',
	computed: {
		total() {
			if (this.selectList.length > 0) {
				return this.selectList
					.reduce((total, current) => {
						return total + parseFloat(current.price_sale) * parseFloat(current.num);
					}, 0)
					.toFixed(2);
			} else {
				return 0;
			}
		},
		isCheckedAll() {
			let arr = this.list.filter((item) => item.kucun > 0);
			return this.selectList.length === arr.length && this.selectList.length > 0;
		}
	},
	data() {
		return {
			del_modal: false,
			NavBar: { height: 0 },
			isedit: false,
			list: [],
			selectList: [],
			keyword: ''
		};
	},
	onLoad() {
		// #ifdef MP-WEIXIN
		this.NavBar = this.$store.state.NavBar;
		// #endif
	},
	onShow() {
		this.isedit = false;
		this.init();
	},
	methods: {
		handleEdit() {
			this.isedit = !this.isedit;
			let arr = this.list.map((item) => {
				item.isCheck = false;
				return item;
			});
			this.list = [];
			this.$nextTick(() => {
				this.list = arr;
			});
		},
		clickName(item) {
			this.toNav(`goods/goodsDetail?id=${item.inventoryId}&inventoryId=${item.inventoryId}&num=${item.num}`);
		},
		clear() {
			this.keyword = '';
			this.init();
		},
		search(e) {
			// 按照姓名字母排序
			this.list = this.list.sort((a, b) => {
				if (a.title.includes(this.keyword) && !b.title.includes(this.keyword)) {
					return -1;
				} else if (!a.title.includes(this.keyword) && b.title.includes(this.keyword)) {
					return 1;
				} else {
					return 0;
				}
			});
		},

		jiesuan() {
			if (this.selectList.length === 0) {
				uni.showToast({
					title: '请选择要结算的商品',
					icon: 'none'
				});
				return;
			}
			uni.setStorageSync('order_info', this.selectList);
			this.toNav(`order/qrOrder`);
		},
		getSelect(list) {
			this.selectList = list;
		},
		quanxuan(e) {
			let isTrue = !!e.detail.value.length;
			if (isTrue != this.isCheckedAll) {
				isTrue = !this.isCheckedAll;
			}
			this.list.forEach((item) => {
				if (item.kucun > 0 || this.isedit) {
					item.isCheck = isTrue;
				}
			});
			console.log(this.isCheckedAll);
		},
		init() {
			API.getJson('gouwuche_lists', { keyword: this.keyword }).then((res) => {
				console.log(res, '列表接口');
				const { code, data, message } = res;
				if (code === 1) {
					if (res.data[0] && res.data[0].list) {
						res.data[0].list.forEach((item) => {
							item.isCheck = false;
						});
						this.list = res.data[0] && res.data[0].list;
					} else {
						this.list = [];
					}
				}
				updata_car_count();
			});
		},

		update(obj) {
			const { e, item } = obj;
			API.getJson('gouwuche_updateNum', {
				inventoryId: item.inventoryId,
				num: e.value
			}).then((res) => {
				console.log(res, 'gouwuche_updateNum');
				this.init();
			});
		},
		shanchu() {
			if (this.selectList.length === 0) {
				uni.showToast({
					title: '请先选择要删除的商品',
					icon: 'none'
				});
				return;
			}

			this.del_modal = true;
		},
		qrdel() {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			let inventoryId = this.selectList.map((item) => item.inventoryId).join(',');
			API.getJson('gouwuche_del', {
				inventoryId
			}).then((res) => {
				uni.hideLoading();
				const { code, data, message } = res;
				updata_car_count();
				this.init();
				uni.showToast({
					title: message,
					icon: 'none'
				});
				this.del_modal = false;
			});
		}
	}
};
</script>

<style lang="less" scoped>
.cart {
	.jiesuanbox {
		height: 96rpx;
	}
	.jiesuan {
		position: fixed;
		z-index: 100;
		bottom: var(--window-bottom);
		display: flex;
		align-items: center;
		width: 750rpx;
		height: 96rpx;
		padding: 0 32rpx;
		background: rgba(255, 255, 255, 0.4);
		backdrop-filter: blur(10rpx); /* 调整模糊程度，例如10px */
		box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);
		.box {
			flex: 1;
			display: flex;
			align-items: center;
			label {
				display: flex;
				align-items: center;
				text {
					margin-left: 10rpx;
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #141414;
				}
			}
		}
		.box3 {
			flex: 1;
			display: flex;
			justify-content: flex-end;
			button {
				margin: 0;
				width: 192rpx;
				height: 80rpx;
				background: #e54e4e;
				border-radius: 40rpx 40rpx 40rpx 40rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #ffffff;
			}
		}
		.box2 {
			display: flex;
			align-items: center;
			text:nth-of-type(1) {
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #222222;
			}
			text:nth-of-type(2) {
				font-size: 44rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #ff0000;
				&:before {
					content: '￥';
					font-size: 28rpx;
				}
			}
		}
	}
	.container {
		padding: 24rpx 32rpx;
	}
}
</style>
